package com.nt.train.controller;
import com.nt.train.dto.Member;
import com.nt.train.service.Strategy;
import com.nt.train.service.context.StrategyContext;
import com.nt.train.service.impl.GeneralMember;
import com.nt.train.service.impl.VipMember;
import com.nt.train.utils.SpringUtil;
import com.nt.train.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 外部调用接口
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private StrategyContext strategyContext;

    /**
     * 普通实现
     * @param member
     * @return
     */
    @RequestMapping("/calculate")
    public ResultVo calculate(@RequestBody Member member) {
        if("generalMember".equals(member.getMemberLevel())){
            Strategy strategy  =  new GeneralMember();
            return  ResultVo.ok(strategy.calculatePrice(member));
        }
        if("vipMember".equals(member.getMemberLevel())){
            Strategy strategy  =  new VipMember();
            return  ResultVo.ok(strategy.calculatePrice(member));
        }
        return null;
    }

    /**
     * 稍微高级一点
     * @param member
     * @return
     */
    @RequestMapping("/calculate2")
    public ResultVo calculate2(@RequestBody Member member) {
        Strategy strategy  = null;
        if("generalMember".equals(member.getMemberLevel())){
             strategy  = SpringUtil.getBean("generalMember",Strategy.class);
            return  ResultVo.ok(strategy.calculatePrice(member));
        }
        if("vipMember".equals(member.getMemberLevel())){
            strategy  = SpringUtil.getBean("vipMember",Strategy.class);
            return  ResultVo.ok(strategy.calculatePrice(member));
        }
        return null;
    }

    /**
     * 高级实现
     * @param member
     * @return
     */
    @RequestMapping("/calculatePriceSerior")
    public ResultVo calculatePrice(@RequestBody Member member) {
        return  ResultVo.ok(strategyContext.calculatePrice(member));
    }
}
